package stagefour.servlet.product;

import com.alibaba.fastjson2.JSONObject;
import stagefour.model.ProductModel;
import stagefour.model.ResultModel;
import stagefour.service.ProductService;
import stagefour.service.impl.ProductServiceImpl;
import stagefour.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.logging.Logger;

@WebServlet("/back/product/add")
public class ProductAddServlet extends HttpServlet {
    static Logger LOGGER = Logger.getLogger("");

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1.乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        2.得到前端的值
        String name = req.getParameter("name");
        String normalPrice = req.getParameter("normalPrice");
        String marketPrice = req.getParameter("marketPrice");
        String content = req.getParameter("content");
        String isShow = req.getParameter("isShow");
        String isRecommend = req.getParameter("isRecommend");
        String imgHref = req.getParameter("imgHref");
        String whereShow = req.getParameter("whereShow");
        String cuisineId = req.getParameter("cuisine");

        LOGGER.info("cuisineId" + cuisineId);
//        3.封装到model
        ProductModel productModel = new ProductModel();
        productModel.setName(Tool.nullToStr(name));
        productModel.setNormalPrice(Tool.nullToDecimal(normalPrice));
        productModel.setMarketPrice(Tool.nullToDecimal(marketPrice));
        productModel.setContent(Tool.nullToStr(content));
        productModel.setIsShow(Tool.strToInt(isShow));
        productModel.setIsRecommend(Tool.strToInt(isRecommend));
        productModel.setImgHref(Tool.nullToStr(imgHref));
        productModel.setWhereShow(Tool.strToInt(whereShow));
        productModel.setCuisineId(Tool.strToInt(cuisineId));
//        4.调用service
        ProductService productService = new ProductServiceImpl();
//        5.调用service的方法并得到返回值
        ResultModel resultModel = productService.add(productModel);

//        6.返回前端
        resp.getWriter().println(JSONObject.toJSONString(resultModel));


    }
}
